/**
 * @author Ahammer
 * @date   2009 07
 * @update 
 * 
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.JCheckBox;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import main.IqmTools;
import main.Tank;

public class IqmCoGRegGUI extends IqmOperatorJFrame 
							  implements IqmOperatorGUIBuilder, ActionListener, ChangeListener {

	private  ParameterBlockJAI     pbJAI       = null;  
	//public  ImgProcFuncJFrame    opGUIFrame  = null;
	//private  IqmCoGregGUI        opGUIFrame  = null;  //actionPerformed events werden in dieser Klasse abgehandelt

	private  JCheckBox 	    jCheckBoxFirst	 = null;
	

	
	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pbJAI
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pbJAI = arg;
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		if (jCheckBoxFirst.isSelected())    pbJAI.setParameter("First", 1);
		if (!jCheckBoxFirst.isSelected())   pbJAI.setParameter("First", 0);		

		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmCoGRegGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
//      String type = IqmTools.getCurrImgTyp(pi);
	       
	    if (pbJAI.getIntParameter("First")  == 0) jCheckBoxFirst.setSelected(false);
	    if (pbJAI.getIntParameter("First")  == 1) jCheckBoxFirst.setSelected(true);

	}
	//-------------------------------------------------------------------------------------------------

	/**
	 * constructor
	 */
	public IqmCoGRegGUI() {

	}
	/**
	 * This method creates and shows the GUI
	 * 
	 */
	@Override
	public void createIqmOperatorGUI() {
		//opGUIFrame = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame = new IqmCoGregGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("CoGReg");  
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height+150);
		this.getJContentPane().add(getJCheckBoxFirst(),   getGridBagConstraintsFirst());	

		//this.repaint();
		if (pbJAI == null) pbJAI = new ParameterBlockJAI("IqmCoGReg");  //nur wenn das erste mal aufgerufen
		//pb = new ParameterBlockJAI("IqmCoGreg"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pbJAI);
		this.setParameterValues();
		this.setParameterBlock();
		
		this.pack();
	}
	
	private  GridBagConstraints getGridBagConstraintsFirst(){
		GridBagConstraints gridBagConstraintsFirst = new GridBagConstraints();
		gridBagConstraintsFirst.gridx = 1;
		gridBagConstraintsFirst.gridy = 4;
		gridBagConstraintsFirst.gridwidth = 3;
		//gridBagConstraintsFirst.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsFirst.insets = new Insets(10,0,20,0); //top left bottom right
		//gridBagConstraintsFirst.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsFirst;
	}

	
	//------------------------------------------------------------------------------------------------------

	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	public void updateIqmOperatorGUI(){
	
	}
	//------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jCheckBoxFirst	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxFirst() {
		if (jCheckBoxFirst == null) {
			jCheckBoxFirst = new JCheckBox();
			jCheckBoxFirst.setText("center 1st image");
			jCheckBoxFirst.addActionListener(this);			
			jCheckBoxFirst.setActionCommand("parameter");	
			jCheckBoxFirst.setEnabled(false);
		}
		return jCheckBoxFirst;
	}

    //----------------------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmCoGregGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmCoGreg".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			//this.setParameterValues();
			this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {		
			if(jCheckBoxFirst == e.getSource()){
			}
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//-------------------------------------------------------------------------------------------------
	@Override
	public void stateChanged(ChangeEvent e) {
		
		this.setParameterBlock();
		this.setParameterValues();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
		
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmCoGRegGUI gui = new IqmCoGRegGUI();
			    gui.createIqmOperatorGUI();

			}
		});
	}
	
}//END
